#include "event_despatcher.h"

event_despatcher::event_despatcher()
{
    m_connect_events.clear();
    m_message_events.clear();
}

void event_despatcher::regist_connect_event(EventKey key, std::function<void(int)> func)
{
    m_connect_events[key] = func;
}

void event_despatcher::regist_message_event(EventKey key, std::function<int(int, const char*, int)> func)
{
    m_message_events[key] = func;
}

void event_despatcher::regist_error_event(EventKey key, std::function<void(int, int)> func)
{
    m_error_events[key] = func;
}

bool event_despatcher::trigger_connect_event(EventKey key, int id)
{
    if (m_connect_events.count(key) > 0)
    {
        m_connect_events[key](id);
        return true;
    }
    return false;
}

int event_despatcher::trigger_message_event(EventKey key, int id, const char* data, int len)
{
	if (m_message_events.count(key) > 0)
	{
        return m_message_events[key](id,data,len);
	}
	return 0;
}

bool event_despatcher::trigger_error_event(EventKey key, int id, int error_code)
{
	if (m_error_events.count(key) > 0)
	{
        m_error_events[key](id,error_code);
		return true;
	}
	return false;
}
